__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import logging
from create_classifier import Category, Word

class GetClassifierJob(webapp.RequestHandler):
	def get(self):
		s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
		s += "<words>"
		q = db.GqlQuery("SELECT * from Category")
		result = q.fetch(1)
		if len(result) > 0:
			s += "<prob_c1>" + str(result[0].prob_c1) + "</prob_c1>"
			s += "<prob_c2>" + str(result[0].prob_c2) + "</prob_c2>"
			s += "<prob_c3>" + str(result[0].prob_c3) + "</prob_c3>"

		q = db.GqlQuery("SELECT * from Word")
		for word in q:
			if not word.content:
				continue
			s += "<word>"
			s += "<content>" + word.content + "</content>"
			s += "<prob>" + str(word.prob) + "</prob>"
			s += "<prob_c1>" + str(word.prob_c1) + "</prob_c1>"
			s += "<prob_c2>" + str(word.prob_c2) + "</prob_c2>"
			s += "<prob_c3>" + str(word.prob_c3) + "</prob_c3>"
			s += "</word>"
		s += "</words>"
		self.response.out.write(s)


application = webapp.WSGIApplication(
	[('/get_classifier', GetClassifierJob)],
	debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
